package com.mdc.mansion.api.web.modules.dishes;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.esotericsoftware.minlog.Log;
import com.mdc.mansion.api.web.common.web.BaseController;
import com.mdc.mansion.common.exception.BusinessException;
import com.mdc.mansion.common.response.Result;
import com.mdc.mansion.common.response.ResultCode;
import com.mdc.mansion.common.util.I18nUtil;
import com.mdc.mansion.core.modules.dishes.RestaurantDishesService;
import com.mdc.mansion.core.modules.dishes.dto.RestaurantDishesDTO;
import com.mdc.mansion.dataaccess.common.context.ContextHolder;
import com.mdc.mansion.dataaccess.modules.dishes.vo.RestaurantDishesVo;

/**
 * 
 * @author xizf
 *
 */
@RequestMapping(value = "${adminPath}/dishes")
@Controller
public class RestaurantDishesController extends BaseController {

    @Resource
    private RestaurantDishesService restaurantDishesService;

	/**
	 * 查询菜品列表信息
	 * 如果没有菜品ID，就查询所有的信息
	 * @param data
	 * @return
	 */
	@RequestMapping(value="/list") 
	public Result findDishesList(String data){
	    JSONObject paramJson = JSONObject.parseObject(data);
		Result restultResult = new Result(ResultCode.COMMON_SUCCESS);
		RestaurantDishesVo vo = new RestaurantDishesVo();
//	    try {
//	        dishesCategoryId = paramJson.getString("dishesCategoryId");
//			vo.setRestaurantDishesCategoryId(dishesCategoryId);
//        } catch (Exception e) {
//
//        }
        if(net.sf.json.JSONObject.fromObject(data).containsKey("dishesCategoryId"))
        {
			vo.setRestaurantDishesCategoryId(paramJson.getString("dishesCategoryId"));
		}
		vo.setRestaurantId(ContextHolder.currentRestaurantId());
//		vo.setRestaurantId("7687d3da4b7944cb9f9b14b9ad2ce647");
		restultResult.setProperty("dishesList",restaurantDishesService.findDishesListWithExt(vo));
		return restultResult;
	}
	
	/**
	 * 菜品删除
	 * @return
	 * 注意.删除需要删除相应的口味/规格
	 */
	@RequestMapping(value = "/delete")
	public Result delete(String data) {
		Result result = new Result(ResultCode.COMMON_SUCCESS);
		JSONObject jsonObject=JSONObject.parseObject(data);
		restaurantDishesService.deleteDishesAndStand(jsonObject.getString("id"));
		return result;
	}
	
	/**
	 * 修改/新增
	 *规格处理需注意
	 *新增/更新 需注意: 调用翻译接口重新处理
	 *还有相应的规格/口味处理
	 */
	@RequestMapping(value = "save") 
	public Result save( String data  ) {
		Result result = new Result(ResultCode.COMMON_SUCCESS);
		try{
			RestaurantDishesDTO restaurantDishesDTO = JSONObject.parseObject(data, RestaurantDishesDTO.class);
	        restaurantDishesService.saveDishes(restaurantDishesDTO);
		}catch(BusinessException e) {
			throw e;
		}catch(Exception e) {
			/**菜品修改/新增失败,请重试**/
			Log.error(e.getMessage());
			throw new BusinessException(I18nUtil.getMessage("dishes.edit.fail.exception"));
		}
		return result;
	}
	
	/**
	 * 传参格式: 菜品ID:排序码,菜品ID:排序码
	 * 以此类推
	 * 处理时只需要将字符串进行split
	 * @return
	 */
	@RequestMapping(value = "sort") 
	public Result updateDisSort(String data)
	{
		Result restultResult = new Result(ResultCode.COMMON_SUCCESS);
		try{
			if(StringUtils.isBlank(data)) {
				throw new BusinessException(I18nUtil.getMessage("dishes.edit.sort.fail.exception"));
			}
			///更新菜品排序码
			restaurantDishesService.updateDisSort(data);
		}catch(BusinessException e) {
			Log.error(e.getMessage());
			throw e;
		}catch(Exception e) {
			/**更新菜品排序失败,请重试**/
			Log.error(e.getMessage());
			throw new BusinessException(I18nUtil.getMessage("dishes.edit.sort.fail.exception"));
		}
		return restultResult;
	}
}
